<?php
namespace Home\Model;
use Think\Model;

class UserModel extends Model {
	protected $_validate = array(
		array('mobile', 'require', '手机号不能为空', 0, 'regex', 1),
		array('mobile', '/^1[3|4|5|8]\d{9}$/', '手机号格式不正确', 0, 'regex', 1),
		array('mobile', 'checkMobileByBoth', '短信手机号和所填手机号不匹配', 0, 'callback', 1),
		array('mobile', 'checkMobileExist', '手机号已被注册', 0, 'callback', 1),
		array('verifycode','require', '验证码不能为空', 0, 'regex', 1),
		array('verifycode','checkTimeVerify', '验证码已过期', 0, 'callback', 1),
		array('verifycode','checkVerify', '验证码不正确', 0, 'callback', 1),
		array('password', 'require', '密码不能为空', 0, 'regex', 1),
		array('repassword', 'checkPwd', '两次密码不一致', 0, 'callback', 1),	
		array('account', 'require', '帐号不能为空', 0, 'regex', 4),
		array('account', 'checkLoginAccount', '帐号不存在', 0, 'callback', 4),
		array('password', 'require', '密码不能为空', 0, 'regex', 4),
		array('password', 'checkLoginPwd', '密码错误', 0, 'callback', 4),
	);

	protected $_auto = array(
		array('password', 'md5Pwd', 1, 'callback'),
		array('createtime', 'time', 1, 'function'),
		array('lastlogintime', 'time', 1, 'function'),
		array('lastloginip', 'get_client_ip', 1, 'function'),
	);

	/**
	 * 检测短信验证码是否正确
	 * @param  [type] $code [description]
	 * @return [type]       [description]
	 */
	protected function checkVerify() {
		$verifyCode = session('smsCode');
		$postVerifyCode = I('post.verifycode');

		if($verifyCode == $postVerifyCode) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检测短信验证码是否过期
	 * @param  [type] $code [description]
	 * @return [type]       [description]
	 */
	protected function checkTimeVerify($code) {
		$expireTime = session('smsTime');

		$sTime = 10 * 60;
		if((time() - $expireTime) < $sTime) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查发送短信的手机号和提交手机号是否相同
	 * @param  [type] $mobile [description]
	 * @return [type]         [description]
	 */
	protected function checkMobileByBoth() {
		$smsMobile = session('smsMobile');
		$postMobile = I('post.mobile');

		if($smsMobile) {
			if($postMobile != $smsMobile) {
				return false;
			} else {
				return true;
			}		
		} else {
			return true;
		}
	}

	/**
	 * 检测手机是否被注册
	 * @return [type] [description]
	 */
	protected function checkMobileExist() {
		$mobile = I('post.mobile');

		$map['mobile'] = $mobile;
		$res = M('UserInfo')->where($map)->getField('uid');
		if($res) 
			return false;
		else 
			return true;
	}

	/**
	 * 加密密码
	 * @param  [type] $password [description]
	 * @return [type]           [description]
	 */
	protected function md5Pwd() {
		$passWord = I('post.password');

		return md5(C('PWD_SALT') . $passWord);
	}

	/**
	 * 检查两次密码是否一致
	 * @return [type] [description]
	 */
	protected function checkPwd() {
		$passWord = I('post.password');
		$rePassWord = I('post.repassword');

		if($passWord != $rePassWord) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 检查帐号是否存在
	 * @return [type] [description]
	 */
	protected function checkLoginAccount() {
		$account = I('post.account');
		if(preg_match('/^1[3|4|5|8]\d{9}$/', $account)) {
			$map['mobile'] = $account;
		} else {
			$map['username'] = $account;
		}

		$res = $this->alias('u')->join('__USER_INFO__ AS ui ON ui.uid = u.uid')
					->where($map)
					->getField('u.uid');
		if($res) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查帐号密码是否正确
	 * @return [type] [description]
	 */
	protected function checkLoginPwd() {
		$account = I('post.account');
		$passWord = I('post.password');

		if(preg_match('/^1[3|4|5|8]\d{9}$/', $account)) {
			$map['mobile'] = $account;
		} else {
			$map['username'] = $account;
		}

		$map['password'] = md5(C('PWD_SALT') . $passWord);

		$res = $this->alias('u')->join('__USER_INFO__ AS ui ON ui.uid = u.uid')
					->field('u.uid, u.username, ui.mobile')
					->where($map)
					->find();
		if($res) {
			session('userinfo', $res);
			return true;
		} else {
			return false;
		}

	}
}
